package com.zw.yzk.kabu.base;

import android.content.Context;
import android.support.multidex.MultiDexApplication;
import android.support.v4.content.ContextCompat;
import cn.finalteam.galleryfinal.*;
import com.tencent.bugly.crashreport.CrashReport;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.data.cache.db.DataBaseManager;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.utils.FileUtils;
import com.zw.yzk.kabu.im.IMUtils;
import com.zw.yzk.kabu.internal.component.ApplicationComponent;
import com.zw.yzk.kabu.internal.component.DaggerApplicationComponent;
import com.zw.yzk.kabu.internal.module.ApplicationModule;
import com.zw.yzk.kabu.jpush.JPushHelper;
import com.zw.yzk.kabu.management.GlideImageLoader;
import com.zw.yzk.kabu.skin.LocalSkinManager;
import com.zw.yzk.kabu.skin.MySkinManager;

import skin.support.SkinCompatManager;

import java.io.File;

/**
 * Created by zhanwei on 2016/12/23.
 */

public class BaseApplication extends MultiDexApplication {

    private ApplicationComponent applicationComponent;
    private static BaseApplication mInstance;


    public static BaseApplication getInstance() {
        if (mInstance == null) {
            mInstance = new BaseApplication();
        }
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        init();

        /* 皮肤设置 */
        SkinCompatManager.init(this)                          // 基础控件换肤初始化
                .loadSkin();                                  // 加载当前皮肤库(保存在SharedPreferences中)
        LocalSkinManager.getInstance().init(this);
        MySkinManager.init(this);
        /* end */

        /* Bugly */ CrashReport.initCrashReport(getApplicationContext()); //CrashReport.testJavaCrash();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        DataBaseManager.getInstance().dispose();
    }

    private void init() {
        mInstance = this;
        applicationComponent = DaggerApplicationComponent.builder().applicationModule(new ApplicationModule(this)).build();
        applicationComponent.inject(this);

        //初始化Retrofit
        ServiceGenerator.initService(this);
        //初始化GreenDao
        DataBaseManager.getInstance().init(this);
        //初始化GalleryFinal
        initGalleryFinal(this);
        //初始化扫描二维码
        ZXingLibrary.initDisplayOpinion(this);
        //初始化极光推送
        JPushHelper.init(this);
        //初始化IM
        IMUtils.init(this);
    }

    //初始化GalleryFinal
    private void initGalleryFinal(Context context) {
        //设置主题
        ThemeConfig theme = new ThemeConfig.Builder()
                .setTitleBarBgColor(ContextCompat.getColor(this, R.color.black))
                .setTitleBarTextColor(ContextCompat.getColor(this, R.color.white))
                .setTitleBarIconColor(ContextCompat.getColor(this, R.color.white))
                .setIconBack(R.drawable.ic_arrow_left_white)
                .build();
        //配置功能
        FunctionConfig functionConfig = new FunctionConfig.Builder()
                .setEnableCamera(true)
                .setEnableEdit(true)
                .setEnableCrop(true)
                .setEnableRotate(true)
                .setCropSquare(true)
                .setEnablePreview(true)
                .build();
        //配置imageLoader
        ImageLoader imageLoader = new GlideImageLoader();
        CoreConfig coreConfig = new CoreConfig.Builder(context, imageLoader, theme)
                .setEditPhotoCacheFolder(new File(FileUtils.getImageEdifDir(this)))
                .setTakePhotoFolder(new File(FileUtils.getPhotographDir(this)))
                .setFunctionConfig(functionConfig)
                .setNoAnimcation(true)
                .build();
        GalleryFinal.init(coreConfig);
    }

    public ApplicationComponent getApplicationComponent() {
        return this.applicationComponent;
    }

}
